{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "source": [
    "import geopandas as gpd;\r\n",
    "import pandas as pd\r\n",
    "import numpy as np"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stderr",
     "text": [
      "C:\\ProgramData\\Anaconda3\\envs\\geo-env\\lib\\site-packages\\geopandas\\_compat.py:106: UserWarning: The Shapely GEOS version (3.9.1dev-CAPI-1.14.1) is incompatible with the GEOS version PyGEOS was compiled with (3.9.1-CAPI-1.14.2). Conversions between both will be slow.\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 随机点"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "source": [
    "import random\r\n",
    "def randomPoint(ptsNum,maxLon,minLon,maxLat,minLat,minZ=0,maxZ=0,crs='4326'):\r\n",
    "    lonRandArray = []\r\n",
    "    latRandArray = []\r\n",
    "    zRandArray = []\r\n",
    "    for i in range(ptsNum):\r\n",
    "        lonRandArray.append(random.uniform(minLon, maxLon))\r\n",
    "        latRandArray.append(random.uniform(minLat, maxLat))\r\n",
    "        if minZ!=maxZ:\r\n",
    "          zRandArray.append(random.uniform(minZ, maxZ))\r\n",
    "    if len(zRandArray)!=0:\r\n",
    "        df = pd.DataFrame({'x': lonRandArray, 'y': latRandArray, 'z': zRandArray})\r\n",
    "    else:\r\n",
    "        df = pd.DataFrame(\r\n",
    "            {'x': lonRandArray, 'y': latRandArray})\r\n",
    "    gdf = gpd.GeoDataFrame(\r\n",
    "        df, geometry=gpd.points_from_xy(df['x'], df['y']),crs=\"EPSG:\"+crs)\r\n",
    "    print(gdf)  \r\n",
    "    return gdf\r\n"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "source": [
    "randomPoint(ptsNum=10,maxLon=120,minLon=100,maxLat=20,minLat=10)"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "            x          y                    geometry\n",
      "0  112.644838  15.418069  POINT (112.64484 15.41807)\n",
      "1  107.923083  12.114463  POINT (107.92308 12.11446)\n",
      "2  105.549660  11.340198  POINT (105.54966 11.34020)\n",
      "3  103.167689  12.022596  POINT (103.16769 12.02260)\n",
      "4  105.156684  14.088153  POINT (105.15668 14.08815)\n",
      "5  106.034792  15.321086  POINT (106.03479 15.32109)\n",
      "6  110.288344  12.892785  POINT (110.28834 12.89279)\n",
      "7  115.489404  17.019084  POINT (115.48940 17.01908)\n",
      "8  119.901974  16.295176  POINT (119.90197 16.29518)\n",
      "9  107.030186  15.178233  POINT (107.03019 15.17823)\n"
     ]
    },
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "            x          y                    geometry\n",
       "0  112.644838  15.418069  POINT (112.64484 15.41807)\n",
       "1  107.923083  12.114463  POINT (107.92308 12.11446)\n",
       "2  105.549660  11.340198  POINT (105.54966 11.34020)\n",
       "3  103.167689  12.022596  POINT (103.16769 12.02260)\n",
       "4  105.156684  14.088153  POINT (105.15668 14.08815)\n",
       "5  106.034792  15.321086  POINT (106.03479 15.32109)\n",
       "6  110.288344  12.892785  POINT (110.28834 12.89279)\n",
       "7  115.489404  17.019084  POINT (115.48940 17.01908)\n",
       "8  119.901974  16.295176  POINT (119.90197 16.29518)\n",
       "9  107.030186  15.178233  POINT (107.03019 15.17823)"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>geometry</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>112.644838</td>\n",
       "      <td>15.418069</td>\n",
       "      <td>POINT (112.64484 15.41807)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>107.923083</td>\n",
       "      <td>12.114463</td>\n",
       "      <td>POINT (107.92308 12.11446)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>105.549660</td>\n",
       "      <td>11.340198</td>\n",
       "      <td>POINT (105.54966 11.34020)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>103.167689</td>\n",
       "      <td>12.022596</td>\n",
       "      <td>POINT (103.16769 12.02260)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>105.156684</td>\n",
       "      <td>14.088153</td>\n",
       "      <td>POINT (105.15668 14.08815)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>106.034792</td>\n",
       "      <td>15.321086</td>\n",
       "      <td>POINT (106.03479 15.32109)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>110.288344</td>\n",
       "      <td>12.892785</td>\n",
       "      <td>POINT (110.28834 12.89279)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>115.489404</td>\n",
       "      <td>17.019084</td>\n",
       "      <td>POINT (115.48940 17.01908)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>119.901974</td>\n",
       "      <td>16.295176</td>\n",
       "      <td>POINT (119.90197 16.29518)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>107.030186</td>\n",
       "      <td>15.178233</td>\n",
       "      <td>POINT (107.03019 15.17823)</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "metadata": {},
     "execution_count": 3
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "source": [
    "randomPoint(ptsNum=10,maxLon=120,minLon=100,maxLat=20,minLat=10,maxZ=100,minZ=10,crs='4490')"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "            x          y          z                    geometry\n",
      "0  104.784389  13.620810  32.588279  POINT (104.78439 13.62081)\n",
      "1  113.844494  15.581699  81.600136  POINT (113.84449 15.58170)\n",
      "2  117.425493  15.766481  99.004085  POINT (117.42549 15.76648)\n",
      "3  102.464027  15.895060  97.501670  POINT (102.46403 15.89506)\n",
      "4  111.181087  14.000514  47.080061  POINT (111.18109 14.00051)\n",
      "5  117.386883  15.728542  42.824998  POINT (117.38688 15.72854)\n",
      "6  104.997780  15.551203  56.678728  POINT (104.99778 15.55120)\n",
      "7  115.768005  13.519830  79.354618  POINT (115.76800 13.51983)\n",
      "8  119.660429  17.159869  13.176776  POINT (119.66043 17.15987)\n",
      "9  101.494540  16.216880  33.562857  POINT (101.49454 16.21688)\n"
     ]
    },
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "            x          y          z                    geometry\n",
       "0  104.784389  13.620810  32.588279  POINT (104.78439 13.62081)\n",
       "1  113.844494  15.581699  81.600136  POINT (113.84449 15.58170)\n",
       "2  117.425493  15.766481  99.004085  POINT (117.42549 15.76648)\n",
       "3  102.464027  15.895060  97.501670  POINT (102.46403 15.89506)\n",
       "4  111.181087  14.000514  47.080061  POINT (111.18109 14.00051)\n",
       "5  117.386883  15.728542  42.824998  POINT (117.38688 15.72854)\n",
       "6  104.997780  15.551203  56.678728  POINT (104.99778 15.55120)\n",
       "7  115.768005  13.519830  79.354618  POINT (115.76800 13.51983)\n",
       "8  119.660429  17.159869  13.176776  POINT (119.66043 17.15987)\n",
       "9  101.494540  16.216880  33.562857  POINT (101.49454 16.21688)"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>z</th>\n",
       "      <th>geometry</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>104.784389</td>\n",
       "      <td>13.620810</td>\n",
       "      <td>32.588279</td>\n",
       "      <td>POINT (104.78439 13.62081)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>113.844494</td>\n",
       "      <td>15.581699</td>\n",
       "      <td>81.600136</td>\n",
       "      <td>POINT (113.84449 15.58170)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>117.425493</td>\n",
       "      <td>15.766481</td>\n",
       "      <td>99.004085</td>\n",
       "      <td>POINT (117.42549 15.76648)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>102.464027</td>\n",
       "      <td>15.895060</td>\n",
       "      <td>97.501670</td>\n",
       "      <td>POINT (102.46403 15.89506)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>111.181087</td>\n",
       "      <td>14.000514</td>\n",
       "      <td>47.080061</td>\n",
       "      <td>POINT (111.18109 14.00051)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>117.386883</td>\n",
       "      <td>15.728542</td>\n",
       "      <td>42.824998</td>\n",
       "      <td>POINT (117.38688 15.72854)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>104.997780</td>\n",
       "      <td>15.551203</td>\n",
       "      <td>56.678728</td>\n",
       "      <td>POINT (104.99778 15.55120)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>115.768005</td>\n",
       "      <td>13.519830</td>\n",
       "      <td>79.354618</td>\n",
       "      <td>POINT (115.76800 13.51983)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>119.660429</td>\n",
       "      <td>17.159869</td>\n",
       "      <td>13.176776</td>\n",
       "      <td>POINT (119.66043 17.15987)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>101.494540</td>\n",
       "      <td>16.216880</td>\n",
       "      <td>33.562857</td>\n",
       "      <td>POINT (101.49454 16.21688)</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "metadata": {},
     "execution_count": 4
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 点转线"
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "随机生成的点，随机选两点构成线"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "source": [
    "from shapely.geometry import LineString, Point"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "随机点"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "source": [
    "gdf=randomPoint(ptsNum=10, maxLon=120, minLon=100, maxLat=20,\r\n",
    "            minLat=10, maxZ=100, minZ=10, crs='4490')\r\n"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "            x          y          z                    geometry\n",
      "0  116.410996  17.135794  44.535605  POINT (116.41100 17.13579)\n",
      "1  103.502568  10.262550  49.974629  POINT (103.50257 10.26255)\n",
      "2  112.050065  13.474130  43.260324  POINT (112.05007 13.47413)\n",
      "3  105.765817  19.731151  35.611791  POINT (105.76582 19.73115)\n",
      "4  101.110135  10.316604  11.870973  POINT (101.11013 10.31660)\n",
      "5  106.531910  11.484249  84.307959  POINT (106.53191 11.48425)\n",
      "6  114.306500  17.128943  75.937294  POINT (114.30650 17.12894)\n",
      "7  119.802411  14.654929  41.495032  POINT (119.80241 14.65493)\n",
      "8  117.162551  10.933901  11.198165  POINT (117.16255 10.93390)\n",
      "9  108.795083  16.087080  70.539471  POINT (108.79508 16.08708)\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "source": [
    "def randomLine(gdf,lineNum,crs='4326'):\r\n",
    "    ptsNum = gdf.shape[0]\r\n",
    "    _gdf = gdf['geometry']\r\n",
    "    linelist = []\r\n",
    "    for i in range(lineNum):\r\n",
    "        p1 = _gdf.iloc[random.randint(0, ptsNum-1)]\r\n",
    "        p2 = _gdf.iloc[random.randint(0, ptsNum-1)]\r\n",
    "        line = LineString([p1, p2])\r\n",
    "        linelist.append(line)\r\n",
    "    geoDataFrame = gpd.GeoDataFrame( geometry = linelist,crs=\"EPSG:\"+crs)\r\n",
    "    return geoDataFrame\r\n",
    "\r\n"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "source": [
    "df = randomPoint(ptsNum=10, maxLon=120, minLon=100, maxLat=20,\r\n",
    "                  minLat=10, maxZ=100, minZ=10, crs='4490')\r\n",
    "linedf=randomLine(gdf=df,lineNum=10)\r\n",
    "linedf.to_json()"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "            x          y          z                    geometry\n",
      "0  100.766908  18.247510  79.119240  POINT (100.76691 18.24751)\n",
      "1  104.708612  13.887779  69.277986  POINT (104.70861 13.88778)\n",
      "2  119.163168  18.290369  58.416198  POINT (119.16317 18.29037)\n",
      "3  102.753574  15.652503  65.013592  POINT (102.75357 15.65250)\n",
      "4  116.777062  11.267545  38.353359  POINT (116.77706 11.26755)\n",
      "5  112.538090  11.276381  32.828114  POINT (112.53809 11.27638)\n",
      "6  102.696204  12.273718  52.719855  POINT (102.69620 12.27372)\n",
      "7  102.791626  17.425110  33.348306  POINT (102.79163 17.42511)\n",
      "8  118.883203  11.712925  72.212168  POINT (118.88320 11.71293)\n",
      "9  107.400736  12.738312  11.786376  POINT (107.40074 12.73831)\n"
     ]
    },
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "                                            geometry\n",
       "0  LINESTRING (102.79163 17.42511, 107.40074 12.7...\n",
       "1  LINESTRING (102.69620 12.27372, 112.53809 11.2...\n",
       "2  LINESTRING (116.77706 11.26755, 102.69620 12.2...\n",
       "3  LINESTRING (118.88320 11.71293, 119.16317 18.2...\n",
       "4  LINESTRING (112.53809 11.27638, 102.69620 12.2...\n",
       "5  LINESTRING (102.75357 15.65250, 112.53809 11.2...\n",
       "6  LINESTRING (112.53809 11.27638, 119.16317 18.2...\n",
       "7  LINESTRING (119.16317 18.29037, 112.53809 11.2...\n",
       "8  LINESTRING (112.53809 11.27638, 104.70861 13.8...\n",
       "9  LINESTRING (116.77706 11.26755, 100.76691 18.2..."
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>geometry</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>LINESTRING (102.79163 17.42511, 107.40074 12.7...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>LINESTRING (102.69620 12.27372, 112.53809 11.2...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>LINESTRING (116.77706 11.26755, 102.69620 12.2...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>LINESTRING (118.88320 11.71293, 119.16317 18.2...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>LINESTRING (112.53809 11.27638, 102.69620 12.2...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>LINESTRING (102.75357 15.65250, 112.53809 11.2...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>LINESTRING (112.53809 11.27638, 119.16317 18.2...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>LINESTRING (119.16317 18.29037, 112.53809 11.2...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>LINESTRING (112.53809 11.27638, 104.70861 13.8...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>LINESTRING (116.77706 11.26755, 100.76691 18.2...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "metadata": {},
     "execution_count": 20
    }
   ],
   "metadata": {}
  }
 ],
 "metadata": {
  "orig_nbformat": 4,
  "language_info": {
   "name": "python",
   "version": "3.8.10",
   "mimetype": "text/x-python",
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "pygments_lexer": "ipython3",
   "nbconvert_exporter": "python",
   "file_extension": ".py"
  },
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.8.10 64-bit ('geo-env': conda)"
  },
  "interpreter": {
   "hash": "7b0d6b4aa929793d88e84d754c187056723d52cecd68845c7dca810866802715"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}